/* 
 * echoserveri.c - An iterative echo server 
 */ 
/* $begin echoserverimain */
#include "./csapp.h"

void echo(int connfd,char *haddrp);

int main(int argc, char **argv) 
{
    int listenfd, connfd, port;
    socklen_t clientlen;
    struct sockaddr_in clientaddr;
    struct hostent *hp;
    char *haddrp;
    if (argc != 2) {
	fprintf(stderr, "usage: %s <port>\n", argv[0]);
	exit(0);
    }
    port = atoi(argv[1]);

    listenfd = Open_listenfd(port);
    while (1) {
	clientlen = sizeof(clientaddr);
	connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);

	/* Determine the domain name and IP address of the client */
	hp = Gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr, 
			   sizeof(clientaddr.sin_addr.s_addr), AF_INET);
	haddrp = inet_ntoa(clientaddr.sin_addr);
	printf("server connected to %s (%s)\n", hp->h_name, haddrp);

	echo(connfd,haddrp);
	Close(connfd);
    }
    exit(0);
}
void echo(int connfd,char *haddrp) 
{
    static char timestr[40];  
    time_t t;  
    struct tm *nowtime;
    time(&t);  
    nowtime = localtime(&t);  
    strftime(timestr,sizeof(timestr),"%Y-%m-%d %H:%M:%S",nowtime);
    size_t n; 
    char buf[MAXLINE]; 
    rio_t rio;

    Rio_readinitb(&rio, connfd);
    while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { //line:netp:echo:eof
	//printf("server received %d bytes\n", (int)n);
	printf("服务器实现者学号:20155234\n");
printf("客户端IP:%s\n",haddrp);
	printf("当前时间:%s\n",timestr);
	printf("\n");
	Rio_writen(connfd, buf, n);
    }
}

/* $end echoserverimain */
